package com.ruoyi.erp.service.impl;

import java.util.List;
import com.ruoyi.common.utils.DateUtils;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.erp.mapper.SettleReceivableMapper;
import com.ruoyi.erp.service.ISettleReceivableService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import com.ruoyi.erp.mapper.ReceivableAccountMapper;
import com.ruoyi.erp.domain.ReceivableAccount;
import com.ruoyi.erp.service.IReceivableAccountService;
import com.ruoyi.common.core.text.Convert;
import org.springframework.transaction.annotation.Transactional;

/**
 * 收款记录Service业务层处理
 * 
 * @author sauger
 * @date 2022-03-29
 */
@Service
public class ReceivableAccountServiceImpl implements IReceivableAccountService 
{
    @Autowired
    private ReceivableAccountMapper receivableAccountMapper;
    @Autowired
    private ISettleReceivableService settleReceivableService;
    @Autowired
    private SettleReceivableMapper receivableMapper;

    /**
     * 查询收款记录
     * 
     * @param id 收款记录主键
     * @return 收款记录
     */
    @Override
    public ReceivableAccount selectReceivableAccountById(Integer id)
    {
        return receivableAccountMapper.selectReceivableAccountById(id);
    }

    /**
     * 查询收款记录列表
     * 
     * @param receivableAccount 收款记录
     * @return 收款记录
     */
    @Override
    public List<ReceivableAccount> selectReceivableAccountList(ReceivableAccount receivableAccount)
    {
        return receivableAccountMapper.selectReceivableAccountList(receivableAccount);
    }

    /**
     * 新增收款记录
     * 
     * @param receivableAccount 收款记录
     * @return 结果
     */
    @Override
    @Transactional
    public int insertReceivableAccount(ReceivableAccount receivableAccount)
    {

        receivableAccount.setCreateTime(DateUtils.getNowDate());
        int iRet = receivableAccountMapper.insertReceivableAccount(receivableAccount);
        if (!StringUtils.isNull(receivableAccount.getSettleReceivable()))
        {
            //外派收款
            receivableAccount.getSettleReceivable().stream().forEach( settleReceivable -> {
                settleReceivable.setReceivableDetailId(receivableAccount.getId());
                settleReceivable.setReceiveTime(receivableAccount.getReceiveTime());
                settleReceivableService.insertSettleReceivable(settleReceivable);
            });

        }
        return iRet;
    }

    /**
     * 修改收款记录
     * 
     * @param receivableAccount 收款记录
     * @return 结果
     */
    @Override
    @Transactional
    public int updateReceivableAccount(ReceivableAccount receivableAccount)
    {
        receivableAccount.setUpdateTime(DateUtils.getNowDate());
        if (!StringUtils.isNull(receivableAccount.getSettleReceivable()))
        {
            //外派收款
            receivableAccount.getSettleReceivable().stream().forEach( settleReceivable -> {
                settleReceivable.setReceivableDetailId(receivableAccount.getId());
                settleReceivable.setReceiveTime(receivableAccount.getReceiveTime());
                settleReceivableService.updateSettleReceivable(settleReceivable);
            });

        }
        return receivableAccountMapper.updateReceivableAccount(receivableAccount);
    }

    /**
     * 批量删除收款记录
     * 
     * @param ids 需要删除的收款记录主键
     * @return 结果
     */
    @Override
    public int deleteReceivableAccountByIds(String ids)
    {
        return receivableAccountMapper.deleteReceivableAccountByIds(Convert.toStrArray(ids));
    }

    /**
     * 删除收款记录信息
     * 
     * @param id 收款记录主键
     * @return 结果
     */
    @Override
    @Transactional
    public int deleteReceivableAccountById(Integer id)
    {

        receivableMapper.deleteSettleReceivableByAccountId(id);
        return receivableAccountMapper.deleteReceivableAccountById(id);
    }

    @Override
    public int approve(String ids, Integer status) {
        return receivableAccountMapper.approve(Convert.toStrArray(ids),status);
    }
}
